﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using Acme.Sakila.EFDAL;
using Acme.Sakila.EFDAL.Entity;
using nHydrate.EFCore.DataAccess;

namespace Acme.Sakila.TestSite
{
	public partial class FilmList : BasePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			grdItem.RowDataBound += new GridViewRowEventHandler(grdItem_RowDataBound);
			this.PagingControl1.ObjectSingular = "Film";
			this.PagingControl1.ObjectPlural = "Films";
			this.PagingControl1.RecordsPerPageChanged += new EventHandler(PagingControl1_RecordsPerPageChanged);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!this.IsPostBack)
			{
				this.Populate();
			}
		}

		#endregion

		#region Methods

		private void Populate()
		{
			using (var context = new SakilaEntities(Utils.ContextStartup))
			{
				List<film> filmCollection = null;
				var paging = new Paging(this.PagingControl1.PageIndex, this.PagingControl1.RecordsPerPage);

				if (this.Request["languageId"] != null)
				{
					int languageId;
					int.TryParse(this.Request["languageId"], out languageId);
					filmCollection = context.film.GetPagedResults(x => x.language_id == languageId, x => x.film_id, paging).ToList();
					var language = context.language.FirstOrDefault(x => x.language_id == languageId);
					lblHeader.Text = "This is a list of all films for language <a href='/LanguageItem.aspx?id=" + language.language_id + "'>" + language.name + "</a>.";
				}
				else if (this.Request["actorId"] != null)
				{
					short actorId;
					short.TryParse(this.Request["actorId"], out actorId);
					var theActor = context.actor.FirstOrDefault(x => x.actor_id == actorId);
					filmCollection = context.film.GetPagedResults(x => x.actorList.Any(z => z.actor_id == actorId), x => x.film_id, paging).ToList();
					lblHeader.Text = "This is a list of all films for actor <a href='/ActorItem.aspx?id=" + theActor.actor_id + "'>" + theActor.first_name + " " + theActor.last_name + "</a>.";
				}
				else if (this.Request["categoryId"] != null)
				{
					short categoryId;
					short.TryParse(this.Request["categoryId"], out categoryId);
					var theCategory = context.category.FirstOrDefault(x => x.category_id == categoryId);
					filmCollection = context.film.GetPagedResults(x => x.categoryList.Any(z=>z.category_id == categoryId), x=>x.film_id, paging).ToList();
					lblHeader.Text = "This is a list of all films for category <a href='/CategoryItem.aspx?id=" + theCategory.category_id + "'>" + theCategory.name + "</a>.";
				}
				else
				{
					filmCollection = context.film.GetPagedResults(x => x.film_id, paging).ToList();
					lblHeader.Text = "This is a list of all films.";
				}

				this.PagingControl1.ItemCount = paging.RecordCount;
				grdItem.DataSource = filmCollection;
				grdItem.DataBind();
				SessionHelper.LastFilmListSearch = this.Request.Url.AbsoluteUri;
			}
		}

		#endregion

		#region Event Handlers

		private void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var theFilm = e.Row.DataItem as Acme.Sakila.EFDAL.Entity.film;
				var linkEdit = (HyperLink)e.Row.FindControl("linkEdit");
				var linkActors = (HyperLink)e.Row.FindControl("linkActors");

				linkEdit.NavigateUrl = "/FilmItem.aspx?id=" + theFilm.film_id;
				linkActors.NavigateUrl = "/ActorList.aspx?filmid=" + theFilm.film_id;
			}
		}

		private void PagingControl1_RecordsPerPageChanged(object sender, EventArgs e)
		{
			var query = new URL(this.Request.Url.AbsoluteUri);
			query.Parameters.SetValue("rpp", PagingControl1.RecordsPerPage.ToString());
			query.Parameters.SetValue("po", "1");
			this.Response.Redirect(query.ToString());
		}

		#endregion

	}
}